package com.qfedu.knowledge.entity.enums;

import com.qfedu.knowledge.entity.vo.QuestionInfoVO;
import com.qfedu.knowledge.strategy.IHighLightStrategy;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;

import java.util.Objects;

/**
 * @ClassName HighLightColumnEnum
 * @Author ykx
 * @Date 2024/5/23 16:54
 * @Version 1.0
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum HighLightColumnEnum implements IHighLightStrategy {

    QUESTION(1, "question") {
        @Override
        public void setHighLight(QuestionInfoVO questionInfoVO, HighlightField highlightField) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < highlightField.fragments().length; i++) {
                result.append(highlightField.fragments()[i]);
            }
            questionInfoVO.setQuestion(result.toString());
        }
    }, OPTION_A(2, "optionA") {
        @Override
        public void setHighLight(QuestionInfoVO questionInfoVO, HighlightField highlightField) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < highlightField.fragments().length; i++) {
                result.append(highlightField.fragments()[i]);
            }
            questionInfoVO.setOptionA(result.toString());
        }
    }, OPTION_B(3, "optionB") {
        @Override
        public void setHighLight(QuestionInfoVO questionInfoVO, HighlightField highlightField) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < highlightField.fragments().length; i++) {
                result.append(highlightField.fragments()[i]);
            }
            questionInfoVO.setOptionB(result.toString());
        }
    };

    /**
     * 编码
     */
    private Integer code;

    /**
     * 处理类型
     */
    private String column;

    public static HighLightColumnEnum getEnumByColumn(String column) {
        for (HighLightColumnEnum value : HighLightColumnEnum.values()) {
            if (Objects.equals(value.getColumn(), column)) {
                return value;
            }
        }
        return null;
    }

    @Override
    public void setHighLight(QuestionInfoVO questionInfoVO, HighlightField highlightField) {

    }

}
